package it.gommonauti.quiz.patente.app;


import it.gommonauti.patenti.entity.Category;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 
 * @author Francesco Stallone ( f.stallone73@gmail.com ).
 *
 * The category list activity.
 */
public class ChooseCategoryActivity extends BaseActivity {

	private static final String DEBUG_TAG= "ChooseCategoryActivity";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		Log.i(DEBUG_TAG, "onCreate()");

		super.onCreate(savedInstanceState);

		String idLanguage = getSelectedLanguageID();
		
		int idExam  = getSelectedExamID();
		
		ArrayList<Category> categories = getCategories(idLanguage, idExam); 
		
		ScrollView mainScrollView = new ScrollView(this);
		LinearLayout mainLinearLayout = new LinearLayout(this);
		mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
		mainScrollView.addView(mainLinearLayout);
		this.setContentView(mainScrollView);
		
		if(categories != null){
			
			for (Category category : categories) {
				
				Button button = new Button(this);
				Typeface tf = Typeface.createFromAsset(getAssets(), "ASTRO867.TTF");
				button.setTypeface(tf);
				
				//TODO : esternalize
				button.setTextColor(Color.rgb(3, 96, 176));
				
				button.setText(category.getDescription()); // TODO exteranalize		
				mainLinearLayout.addView(button);
				button.setEnabled(true);
				button.setTag(new Integer(category.getId()));
				button.setOnClickListener(buttonOnClickHandler);
				
			}
			
		} else {
			
			TextView textView = new TextView(this);
			textView.setText("SORRY: NO CATEGORY IN DB");
			mainLinearLayout.addView(textView);
			
		}
		

	}
	
	/**
	 * Button on click listener.
	 */
	private View.OnClickListener buttonOnClickHandler = new View.OnClickListener() {
		public void onClick(View v) {

			onButtonClick(v);

		}	
	};
	
	private void onButtonClick(View v) {
		
		finish();

		Intent intent = new Intent();
		intent.setClass(ChooseCategoryActivity.this, CategoryQuizActivity.class);
		
		Integer selectedCategoryID = (Integer) v.getTag(); 				
		setSelectedCategoryID(selectedCategoryID.intValue());
		
		startActivity(intent);	
		
	}

	@Override
	protected String getDebugTag() {
		return DEBUG_TAG;
	}		
}